<?

/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$srcDbHdr        = "C_B_HDR";
$srcDbDtl        = "C_B_DTL";
$srcDbSubDtl     = "C_B_SUB_DTL";
$srcDbHdrLog     = "LOG_C_B_HDR";
$srcDbSequence   = "MST_SEQUENCE";

$UID     = unserialize($_SESSION["serUID"]);
$userId  = $UID->getUID();

$Db = new oracleDb;
$Db->init($_SESSION["ORCL"],
           $_SESSION["OPASS"],
           $_SESSION["SID"],
           _NO_COMMENT);

if($Db->connect()){

	/**
	* Delete the previous entry
	*
	*/
	$CHandlers->dbEntDelete($Db,
            	            $_SESSION["jid"],
            	            $srcDbHdr);

  $CHandlers->dbEntDelete($Db,
            	            $_SESSION["jid"],
            	            $srcDbDtl);

  $CHandlers->dbEntDelete($Db,
                          $_SESSION["jid"],
                          $srcDbSubDtl);

  /**
  * We don't build up any jobId anymore since this is
  * a modify mode. Also we already have the previous
  * jobId in $_SESSION
  *
  */
  $jobId   = $_SESSION["jid"];

  $hdrDate = "TO_DATE('".$_POST["trDateEnt"]."' , 'dd-mm-YYYY')";

  for($iRow = 0; $iRow < $dataup ; $iRow++){

    $values = " '".$jobId."',   ".
              "  ".($iRow+1).", ".
              " '".$_POST["glAccCodeEnt"][$iRow]."',    ".
              " '".$_POST["glSubAccCodeEnt"][$iRow]."', ".
              " '".$_POST["descpEnt"][$iRow]."',        ".
              "  ".$_POST["amountEnt"][$iRow].",        ".
              "  ".$_POST["netEnt"][$iRow]."            ";

    $CLHandlers->insToCbSubDtl($Db,
                               $values,
                               $srcDbSubDtl);
  }

  $values = " '".$jobId."',             ".
            "  ".$_POST["totAmount"].", ".
            "  ".$_POST["totNet"]."     ";

  $CLHandlers->insToCbDtl($Db,
                          $values,
                          $srcDbDtl);

  $CFetchers->fetchHdrNo($Db,
                         $_POST["trDateEnt"],
                         $srcDbHdr,
                         $hdrNo);

  $CFetchers->fetchTxTypeCode($Db,
                              $_POST["txEnt"],
                              $SourceDb->fetchValue("srcDbTxType"),
                              $txCode);

  $values = " '".$jobId."',   ".
            " ' ',            ".
            "  ".$hdrDate.",  ".
            "  ".$hdrNo.",    ".
            " '"._NOT_ACTIVE."',              ".
            " '".$_POST["cbCode"]."',         ".
            " '".$_POST["typeEnt"]."',        ".
            " '".$txCode."',                  ".
            " '".$_POST["currencyEnt"]."',    ".
            "  ".$_POST["rateEnt"].",         ".
            " '".$_POST["suppCstCodeEnt"]."', ".
            " '".$_POST["memoEnt"]."'         ";

  $CLHandlers->insToCbHdr($Db,
                          $values,
                          $srcDbHdr);

  /**
   * transfer process
   * tmp --> real
   */
  switch($_POST["typeEnt"]){

    case _CASH_BANK_IN:

      switch($_POST["txEnt"]){

        case _CASH_BANK_RGL_NAME:

          /* Do nothing */
          break;

        case _CASH_BANK_INVOICE_NAME:


          break;

        case _CASH_BANK_CCL_DP_NAME:


          break;

        case _CASH_BANK_DP_NAME:

          /* Do nothing */
          break;

        default:
          $Misc->usrDieJs("Error: Unknown Tx");
      }
      break;

    case _CASH_BANK_OUT:

      switch($_POST["txEnt"]){

        case _CASH_BANK_RGL_NAME:

          /* Do nothing */
          break;

        case _CASH_BANK_INVOICE_NAME:

          $CHandlers->dbEntDelete($Db,
                                  $jobId,
                                  $SourceDb->fetchValue("srcDbDtlIvcSupplier"));

          $CHandlers->dbEntDelete($Db,
                                  $jobId,
                                  $SourceDb->fetchValue("srcDbSubIvcSupplier"));

          $CLHandlers->transIvcTmpToRealSupplier($Db,
                                                 $jobId,
                                                 $_SESSION["tmpJid"],
                                                 $SourceDb);

          $CHandlers->dbEntDelete($Db,
                                  $_SESSION["tmpJid"],
                                  $SourceDb->fetchValue("srcDbTmpDtlIvcSupplier"));

          $CHandlers->dbEntDelete($Db,
                                  $_SESSION["tmpJid"],
                                  $SourceDb->fetchValue("srcDbTmpSubIvcSupplier"));
          break;

        case _CASH_BANK_CCL_DP_NAME:

          $CHandlers->dbEntDelete($Db,
                                  $jobId,
                                  $SourceDb->fetchValue("srcDbDtlCclDpSupplier"));

          $CHandlers->dbEntDelete($Db,
                                  $jobId,
                                  $SourceDb->fetchValue("srcDbSubCclDpSupplier"));

          $CLHandlers->transCclDpTmpToRealSupplier($Db,
                                                   $jobId,
                                                   $_SESSION["tmpJid"],
                                                   $SourceDb);

          $CHandlers->dbEntDelete($Db,
                                  $_SESSION["tmpJid"],
                                  $SourceDb->fetchValue("srcDbTmpDtlCclDpSupplier"));

          $CHandlers->dbEntDelete($Db,
                                  $_SESSION["tmpJid"],
                                  $SourceDb->fetchValue("srcDbTmpSubCclDpSupplier"));
          break;

        case _CASH_BANK_DP_NAME:

          /* Do nothing */
          break;

        default:
          $Misc->usrDieJs("Error: Unknown Tx");
      }
      break;

    default:
      $Misc->usrDieJs("Error: Unknown Type");
  }

  /**
  * Insert Log
  *
  */
  $CHandlers->insToDbLog($Db,
                         $today_date,
                         $jobId,
                         $userId,
                         $_SERVER["REMOTE_ADDR"],
                         _ENTRY_MODE,
                         $srcDbHdrLog);

	$Db->commit();

	if($Db->getStatus() == true){

	  $Misc->usrMsg("Modify Complete");
	}else{

	  $Misc->usrDieJs("Modify Process Interupted");
	}

	$Db->dc();

	/**
	* Pending...
	* I'm not sure if this
	* reset procedure off
	* causes buggy
	*
	*	$restore  = 0;
	* $uprun    = 0;
	* $dataup   = 0;
	*
	*/
	//jumpTo(2,"../".$_SESSION["source_caller"]);

}else{

	$Misc->usrDieJs("Db connection failed");
}

?>